import java.sql.*;
import java.util.Scanner;

public class Transfer {
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/finance?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC";
    static final String USER = "root";
    static final String PASS = "123123";
    /**
     * 转账操作
     *
     * @param connection 数据库连接对象
     * @param sourceCard 转出账号
     * @param destCard 转入账号
     * @param amount  转账金额
     * @return boolean
     *   true  - 转账成功
     *   false - 转账失败
     */
    public static boolean transferBalance(Connection connection,
                             String sourceCard,
                             String destCard, 
                             double amount)
    {
        try
        {
            connection.setAutoCommit(false);
            connection.setTransactionIsolation(8);
            boolean succeed = false;
            PreparedStatement ps = connection.prepareStatement("select * from bank_card where b_number = ?");
            ps.setString(1, destCard);
            ResultSet res1 = ps.executeQuery();
            connection.commit();
            succeed = res1.next(); // 检查转入账户是否存在
            ps = connection.prepareStatement("select * from bank_card where b_number = ? and b_type = \"储蓄卡\" and b_balance >= ?"); // 检查转出账户是否符合要求
            ps.setString(1, sourceCard);
            ps.setDouble(2, amount);
            ResultSet res2 = ps.executeQuery();
            connection.commit();
            succeed &= res2.next();
            if(succeed)
            {
                Double new_amount1 = res1.getDouble("b_balance") + amount; // 转入账户储蓄卡余额
                if (res1.getString("b_type").equals("信用卡"))
                {
                    new_amount1 = res1.getDouble("b_balance") - amount; // 转入账户信用卡余额
                }
                Double new_amount2 = res2.getDouble("b_balance") - amount; // 转出账户余额
                String update_sql = "update bank_card set b_balance = ? where b_number = ?";
                ps = connection.prepareStatement(update_sql);
                ps.setDouble(1, new_amount1);
                ps.setString(2, destCard);
                ps.executeUpdate();
                ps = connection.prepareStatement(update_sql);
                ps.setDouble(1, new_amount2);
                ps.setString(2, sourceCard);
                ps.executeUpdate();
                connection.commit();
            }
            return succeed;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return false;
    }

    // 不要修改main() 
    public static void main(String[] args) throws Exception {

        Scanner sc = new Scanner(System.in);
        Class.forName(JDBC_DRIVER);

        Connection connection = DriverManager.getConnection(DB_URL, USER, PASS);

        while(sc.hasNext())
        {
            String input = sc.nextLine();
            if(input.equals(""))
                break;

            String[]commands = input.split(" ");
            if(commands.length ==0)
                break;
            String payerCard = commands[0];
            String  payeeCard = commands[1];
            double  amount = Double.parseDouble(commands[2]);
            if (transferBalance(connection, payerCard, payeeCard, amount)) {
              System.out.println("转账成功。" );
            } else {
              System.out.println("转账失败,请核对卡号，卡类型及卡余额!");
            }
        }
    }

}
